package com.app.ytong.manager.qing;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.app.ytong.basis.BasisActivity;
import com.app.ytong.basis.BasisApp;
import com.app.ytong.basis.Constants;
import com.app.ytong.bean.BasisBean;
import com.app.ytong.bean.GoodsListBean;
import com.app.ytong.bean.GoodsListData;
import com.app.ytong.bean.GoodsSubmitBean;
import com.app.ytong.bean.GoodsTypeListBean;
import com.app.ytong.bean.LoginBean;
import com.app.ytong.bean.RecordListBean;
import com.app.ytong.bean.RecordListData;
import com.app.ytong.bean.SupplierListBean;
import com.app.ytong.bean.SupplierListData;
import com.app.ytong.bean.TraceListData;
import com.app.ytong.bean.TraderListBean;
import com.app.ytong.bean.TraderListData;
import com.app.ytong.bean.UserListData;
import com.app.ytong.greendao.GreenDaoUtils;
import com.app.ytong.util.MyUtils;
import com.app.ytong.util.SoftKeyboardUtil;
import com.app.ytong.util.SplitUtil;
import com.app.ytong.util.UnitUtil;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.caysn.autoreplyprint.AutoReplyPrint;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.google.gson.Gson;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.LongByReference;
import com.tencent.bugly.crashreport.biz.UserInfoBean;
import com.vandin.weigher.sdk.connector.IWeigh;
import com.vandin.weigher.sdk.connector.IWeighCallback;
import com.vandin.weigher.sdk.connector.WeightData;
import com.vandin.weigher.sdk.weigher.WeightSerialPort;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import my.DialogUtils;
import my.LogUtil;
import my.MathUtils;
import my.MySharedPreferences;
import my.SystemParamsUtils;
import my.TimeUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

public class ScalePresenterImpl implements ScalePresenter,IWeighCallback{

    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    String oldWeight="0";
    String scaleWeight="0";  //真实的秤重量

    private IWeigh iWeigh;
    ScaleView scaleView;
    MySharedPreferences mSp;

    //静默拍照参数

    private Pointer h = Pointer.NULL;

    public ScalePresenterImpl(ScaleView scaleView){
        this.scaleView = scaleView;
    }

    TraderListBean traderListBean;
    @Override
    public void isJiJiaYuan(BasisActivity mContext) {
        QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(mContext);
        builder.setTitle("请计价员刷卡")
                .setSkinManager(QMUISkinManager.defaultInstance(mContext))
                .setPlaceholder("输入或者扫描卡片")
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        new Handler().postDelayed(new  Runnable(){
                            public  void  run() {
                                SoftKeyboardUtil.hideKeyboard(mContext);
                            }
                        },  100 );
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        String text = builder.getEditText().getText().toString();
                        if (!TextUtils.isEmpty(text)) {

                            isJiJiaYuan(mContext,text);

                            dialog.dismiss();
                            new Handler().postDelayed(new  Runnable(){
                                public  void  run() {
                                    SoftKeyboardUtil.hideKeyboard(mContext);
                                }
                            },  100 );
                        }
                    }
                })
                .create(mCurrentDialogStyle).show();

    }

    @Override
    public void isJiJiaYuan(BasisActivity mContext,String no) {
        if (TextUtils.isEmpty(no)) {
            scaleView.isJiJiaYuan(false,null);
            return;
        }
        traderListBean = GreenDaoUtils.getTraderListBean();
        if(traderListBean.isTraderByCardNo(no)){
            scaleView.isJiJiaYuan(true,traderListBean.getTraderByNo(no));
        }else {
            scaleView.isJiJiaYuan(false,null);
        }


    }





    @Override
    public void getGoodsListByTid(BasisActivity activity, String Tid) {
        MyParams params = new MyParams();
        activity.showProgress();
        params.put("aid", LoginBean.getInstance().getAid());
        params.put("ifs", MyUtils.getHomeSelectId());
        params.put("tid", Tid);

        MyHttpUtils.get(Constants.URL_GOODS_SELECT_LEAF_ALL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (activity.isFinishing()) return;
                        GoodsListBean goodsListBean = (GoodsListBean) result;
//                        scaleView.httpResultGoodsList(goodsListBean);
                        showGoodsOption(activity,goodsListBean);
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        if (activity.isFinishing()) return;
                        super.onFailure(httpWhat, result);
//                        DialogUtils.showErrorDialog(activity,((BasisBean) result).getStatusInfo());
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        activity.dismissProgress();
                    }
                },
                0, GoodsListBean.class);

    }


    public void showGoodsOption(Context mContext,GoodsListBean goodsListBean) {



        String[] items = goodsListBean.getAllNameList().toArray(new String[goodsListBean.getAllNameList().size()]);
        new QMUIDialog.CheckableDialogBuilder(mContext)
                .setSkinManager(QMUISkinManager.defaultInstance(mContext))
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        GoodsListData goodsData = goodsListBean.getList().get(which);
                        scaleView.getGoodsOptionsResult(goodsData);
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();


//        OptionsPickerView pickerView = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                GoodsListData goodsData = goodsListBean.getList().get(options1);
//                scaleView.getGoodsOptionsResult(goodsData);
//            }
//        })
//                .setSubmitText("确定")//确定按钮文字
//                .setCancelText("取消")//取消按钮文字
//                .build();
//        pickerView.setPicker(goodsListBean.getAllNameList());
//        pickerView.show();


    }

    @Override
    public void getSupplierInfoByCode(ScaleActivity activity,String code) {
        MyParams params = new MyParams();
        activity.showProgress();
        params.put("cardno", code);
        params.put("aid", LoginBean.getInstance().getAid());
        MyHttpUtils.get(Constants.URL_PURCHASE_SELECT_BY_CARD, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (activity.isFinishing()) return;
                        SupplierListData data = (SupplierListData) result;
                        scaleView.getSupplierResult(data);
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        if(activity.isFinishing())return;
                        try{
                            DialogUtils.showErrorDialog(activity,((BasisBean) result).getStatusInfo());
                        }catch (Exception e){
                            DialogUtils.showErrorDialog(activity,"异常！");
                            throw e;
                        }
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        activity.dismissProgress();
                    }
                },
                0, SupplierListData.class);
    }




    //----------------以下秤的接口------------------

    @Override
    public void onConnect(int status, String str, boolean b) {
        /**
         * status =1 ; str =正在连接……  ; b = true
         * status =2 ; str =连接成功    ; b = true
         * status =7 ; str =暂停读取    ; b = false
         * status =4 ; str =断开成功    ; b = true
         * status =10 ; str =读取重量    ; b = false           IControl.GetWeight();调用就会回调这个
         */
//        LogUtil.d("ScaleActivity==onConnect==status =="+ status);
//        LogUtil.d("ScaleActivity==onConnect==str =="+ str);
//        LogUtil.d("ScaleActivity==onConnect==b =="+ b);

        switch (status){
            case 2:
                iWeigh.GetWeight();
                break;
        }
    }

    @Override
    public void onReceiveData(String frame, String weight, boolean isSta) {
        //weight的格式 ====      +   0.515
        LogUtil.d("ScaleActivity==weight =="+ weight);
        if(weight.contains("-")){
            scaleView.dashboardViewSetValue(0);
            scaleWeight = String.valueOf(0);
            return;
        }
        if(oldWeight.equals(weight)){
            return;
        }else {
            oldWeight = weight;
        }

        try {
            String  a =  UnitUtil.getNumByString(weight);
            float scale = Float.parseFloat(MathUtils.multiply(a,"2"));
            scaleView.dashboardViewSetValue(scale);
            scaleWeight = String.valueOf(scale);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }


    @Override
    public IWeigh getIWeigh(Context mContext) {
        if(mSp == null){
            mSp = new MySharedPreferences(mContext);
        }
        if (iWeigh != null) {
            iWeigh.disconnect();
        }
        WeightData weightData = new WeightData();
        weightData.setPort(mSp.getPort());
        weightData.setBaudrate(mSp.getBaudrate());
        iWeigh = new WeightSerialPort(mContext, this, weightData);
        iWeigh.OpenScaleUart();
        return iWeigh;
    }

    @Override
    public String getScaleWeight() {
        return scaleWeight;
    }

    @Override
    public void closeIWeigh() {
        if (iWeigh != null) {
            iWeigh.StopRead();
            iWeigh.disconnect();
            iWeigh = null;
        }
    }

    @Override
    public void settlement(BasisActivity activity, GoodsListBean goodsListBean, SupplierListData supplierData, TraderListData curJiJiaYuan, GoodsTypeListBean goodsTypeListBean) {
        if(supplierData == null){
            DialogUtils.showWarningDialog(activity,"供应商请刷卡");
            return;
        }

        if(goodsListBean == null || goodsListBean.getList().size()<1){
            DialogUtils.showWarningDialog(activity,"请添加数据");
            return;
        }

        if(curJiJiaYuan == null || TextUtils.isEmpty(curJiJiaYuan.getUno())){
            curJiJiaYuan = new TraderListData();
            curJiJiaYuan.setUno(LoginBean.getInstance().getUno());
            curJiJiaYuan.setUname(LoginBean.getInstance().getUname());
        }

//        switch (supplierData.getIstra()){
//            case "1":  //代理商
//                if(goodsTypeListBean.getAllTraIsEmpty()){
//                    DialogUtils.showErrorDialog(activity,"请填写代理商溯源重量");
//                    return;
//                }
//
//                String mes = goodsListBean.check(supplierData,goodsTypeListBean);
//                if(!TextUtils.isEmpty(mes)){
//                    DialogUtils.showWarningDialog(activity, ""+mes, "继续", new SweetAlertDialog.OnSweetClickListener() {
//                        @Override
//                        public void onClick(SweetAlertDialog sweetAlertDialog) {
//                            sweetAlertDialog.dismiss();
//                            goodsListBean.resetSupplierData(supplierData,goodsTypeListBean);
//                            submit(activity,goodsListBean,supplierData);
//                        }
//                    });
//                    return;
//                }
//                goodsListBean.resetSupplierData(supplierData,goodsTypeListBean);
//                submit(activity,goodsListBean,supplierData);
//                break;
//
//            default:
//                String message = goodsListBean.check(supplierData,goodsTypeListBean);
//                if(!TextUtils.isEmpty(message)){
//                    DialogUtils.showWarningDialog(activity,message);
//                    return;
//                }
//                submit(activity,goodsListBean,supplierData);
//                break;
//        }

        goodsListBean.resetSupplierData(supplierData,goodsTypeListBean);
        submit(activity,goodsListBean,supplierData,curJiJiaYuan);

    }
    private void submit(BasisActivity activity,GoodsListBean goodsListBean, SupplierListData supplierData,TraderListData curJiJiaYuan){

        goodsListBean.toComplete(LoginBean.getInstance().getRightById(MyUtils.getHomeSelectId()).getFsgByFromIn(),activity,supplierData,curJiJiaYuan,"");
        MyParams params = new MyParams();
        Gson gson = new Gson();
        String a = gson.toJson(goodsListBean.getList());
        params.putJson(a);

        String url ="";
        if(MyUtils.getTabSelectName().contains("入")){
            url = Constants.URL_INSTOCK_INSERT;
        }else {
            url = Constants.URL_OUTSTOCK_INSERT;
        }

        MyHttpUtils.postJson(url, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                if(activity.isFinishing())return;
                GoodsListBean resultGoodListBean = (GoodsListBean) result;
                insertTraces(activity,goodsListBean,resultGoodListBean, supplierData,curJiJiaYuan);
            }

            @Override
            public void onFailure(int httpWhat, Object result) {
                if(activity.isFinishing())return;
//                super.onFailure(httpWhat, result);
                if(result != null &&!TextUtils.isEmpty(((BasisBean) result).getStatusInfo())){
                    scaleView.printMessage(((BasisBean) result).getStatusInfo());
                }else {
                    scaleView.printMessage("异常！");
                }
            }

            @Override
            public void onFinish(int httpWhat) {

            }
        }, 0, GoodsListBean.class);

    }


    @Override
    public void editRecord(BasisActivity activity, RecordListBean recordListBean) {
        subRecord(activity,recordListBean);
    }

    private void subRecord(BasisActivity activity,RecordListBean recordListBean){

        MyParams params = new MyParams();
        Gson gson = new Gson();
        String a = gson.toJson(recordListBean.getList());
        params.putJson(a);

        String url ="";
        if(MyUtils.getTabSelectName().contains("入")){
            url = Constants.URL_INSTOCK_INSERT;
        }else {
            url = Constants.URL_OUTSTOCK_INSERT;
        }

        MyHttpUtils.postJson(url, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                if(activity.isFinishing())return;
                RecordListBean resultGoodListBean = (RecordListBean) result;
                activity.showToast("保存成功");
                scaleView.settlementSuccess();
            }

            @Override
            public void onFailure(int httpWhat, Object result) {
                if(activity.isFinishing())return;
//                super.onFailure(httpWhat, result);
                if(result != null &&!TextUtils.isEmpty(((BasisBean) result).getStatusInfo())){
                    scaleView.printMessage(((BasisBean) result).getStatusInfo());
                }else {
                    scaleView.printMessage("异常！");
                }
            }

            @Override
            public void onFinish(int httpWhat) {

            }
        }, 0, RecordListBean.class);

    }


    private void insertTraces(BasisActivity activity,GoodsListBean goodListBean,GoodsListBean resultGoodListBean,SupplierListData supplierListData,TraderListData curJiJiaYuan){
        MyParams params = new MyParams();
        Gson gson = new Gson();
        String a = gson.toJson(supplierListData.getTracelist());
        params.putJson(a);

        MyHttpUtils.postJson(Constants. URL_TRACES_INSERT, params, new MyHttpListener() {
            @Override
            public void onSuccess(int httpWhat, Object result) {
                if(activity.isFinishing())return;
//                SupplierListData resultSupplierListData = (SupplierListData) result;
                toPrint(activity,goodListBean,resultGoodListBean,supplierListData,curJiJiaYuan);
                scaleView.settlementSuccess();
            }

            @Override
            public void onFailure(int httpWhat, Object result) {
                if(activity.isFinishing())return;
//                super.onFailure(httpWhat, result);
                if(result != null &&!TextUtils.isEmpty(((BasisBean) result).getStatusInfo())){
                    scaleView.printMessage(((BasisBean) result).getStatusInfo());
                }else {
                    scaleView.printMessage("异常！");
                }
            }

            @Override
            public void onFinish(int httpWhat) {

            }
        }, 0, SupplierListData.class);
    }



    //----------------以下都是打印的接口------------------
    @Override
    public void showRecord( BasisActivity activity,RecordListBean recordListBean){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Test_Pos_SampleTicket_58MM_1(activity, recordListBean);
                scaleView.printFinish();
            }
        }).start();
    }
    private void Test_Pos_SampleTicket_58MM_1(BasisActivity activity,RecordListBean recordListBean) {
        Pointer h = OpenPort();
        if (h != Pointer.NULL) {
            RecordListData recordListData = recordListBean.getList().get(0);
            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(h);
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(h);
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(h, AutoReplyPrint.CP_MultiByteEncoding_UTF8);

            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_HCenter);
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 1, 0);
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, ""+LoginBean.getInstance().getAname()+"\r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, ""+ MyUtils.getTabSelectName() +"\r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "————————————————\n");

            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_Left);
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 0);
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "秤编号:  "+ SystemParamsUtils.getAndroidID(activity)+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "凭证号:  "+recordListData.getMid()+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "日期:  "+ TimeUtils.getCurrentTimeByFormat(TimeUtils.TIME_FOMART_DAY)+"\n");
            if(MyUtils.getTabSelectName().contains("采购") || MyUtils.getTabSelectName().contains("销售")){
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "供应商:  "+ recordListData.getCna()+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "身份证:  "+ recordListData.getCno()+"\n");
            }
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "--------------------------------\n");


            for (RecordListData data : recordListBean.getList()){
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "品名:  "+ data.getGna()+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "净重:  "+ data.getGwt()+"斤"+"\n");
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "单价:  "+ data.getPri()+"元"+"\n");
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "金额:  "+ MathUtils.multiply(data.getGwt(),data.getPri())+"元"+"\n");
                if(!(MyUtils.getHomeSelectName().contains("茶青") && MyUtils.getTabSelectName().contains("入"))){
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "箱码:  "+ data.getRfid()+"\n");
                }else {
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "去皮:  "+ data.getPwt()+"斤"+"\n");
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "累加:  "+ data.getAcc()+"\n");
                }
                if(MyUtils.getHomeSelectName().contains("饼")){
                    AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "数量:  "+ data.getGnu()+"\n");
                }

                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "--------------------------------\n");
            }

            if(MyUtils.getTabSelectName().contains("采购") || MyUtils.getTabSelectName().contains("销售")){
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "金额合计:  "+ recordListBean.getAllMoney()+"元"+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "重量合计:  "+ recordListBean.getAllWeight()+"斤"+"\n");

            }

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "操作员:"+ LoginBean.getInstance().getUname());


            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");


            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(h, 2);
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeHeight(h, 60);
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeReadableTextFontType(h, 0);
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeReadableTextPosition(h, AutoReplyPrint.CP_Pos_BarcodeTextPrintPosition_BelowBarcode);
            AutoReplyPrint.INSTANCE.CP_Pos_PrintBarcode(h, AutoReplyPrint.CP_Pos_BarcodeType_CODE93, ""+recordListData.getMid());

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");



            AutoReplyPrint.INSTANCE.CP_Pos_Beep(h, 1, 500);

            QueryPrintResult(h);
            AutoReplyPrint.INSTANCE.CP_Port_Close(h);
        }
    }



    public void toPrint( BasisActivity activity,GoodsListBean goodsListBean,GoodsListBean resultGoodListBean,SupplierListData supplierListData,TraderListData curJiJiaYuan){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Test_Pos_SampleTicket_58MM_1(activity,goodsListBean,resultGoodListBean,supplierListData,curJiJiaYuan);
                scaleView.printFinish();
            }
        }).start();
    }


    private void Test_Pos_SampleTicket_58MM_1(BasisActivity activity,GoodsListBean goodsListBean,GoodsListBean resultGoodListBean, SupplierListData supplierListData,TraderListData curJiJiaYuan) {
        Pointer h = OpenPort();
        if (h != Pointer.NULL) {

            AutoReplyPrint.INSTANCE.CP_Pos_ResetPrinter(h);
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteMode(h);
            AutoReplyPrint.INSTANCE.CP_Pos_SetMultiByteEncoding(h, AutoReplyPrint.CP_MultiByteEncoding_UTF8);

            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_HCenter);
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 1, 0);
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, ""+LoginBean.getInstance().getAname()+"收购凭证\r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "————————————————\n");

            AutoReplyPrint.INSTANCE.CP_Pos_SetAlignment(h, AutoReplyPrint.CP_Pos_Alignment_Left);
            AutoReplyPrint.INSTANCE.CP_Pos_SetTextScale(h, 0, 0);
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "秤编号:  "+ SystemParamsUtils.getAndroidID(activity)+"\n");
//            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "凭证号:  "+submitBean.getTid()+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "日期:  "+ TimeUtils.getCurrentTimeByFormat(TimeUtils.TIME_FOMART_DAY)+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "供应商:  "+ goodsListBean.getCna()+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "身份证:  "+ goodsListBean.getCno()+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "--------------------------------\n");


            for (GoodsListData goodsData : goodsListBean.getList()){
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "品名:  "+ goodsData.getGna()+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "净重:  "+ goodsData.getGwt()+"斤"+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "去皮:  "+ goodsData.getPwt()+"斤"+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "单价:  "+ goodsData.getPri()+"元"+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "金额:  "+ MathUtils.multiply(goodsData.getGwt(),goodsData.getPri())+"元"+"\n");
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "累加:  "+ goodsData.getAcc()+"\n");

                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "--------------------------------\n");
            }

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "金额合计:  "+ goodsListBean.getAllTotal()+"元"+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "重量合计:  "+ goodsListBean.getAllWeight()+"斤"+"\n");

            for(TraceListData data : supplierListData.getTracelist()){
                AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, data.getTna()+"余量:  "+ data.getTra()+"\n");
            }
//            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "称重员:"+ LoginBean.getInstance().getUname()+"   计价员 :"+ LoginBean.getInstance().getUname()+"\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "称重员:"+ LoginBean.getInstance().getUname()+"   计价员 :"+ curJiJiaYuan.getUname()+"\n");


            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");


            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeUnitWidth(h, 2);
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeHeight(h, 60);
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeReadableTextFontType(h, 0);
            AutoReplyPrint.INSTANCE.CP_Pos_SetBarcodeReadableTextPosition(h, AutoReplyPrint.CP_Pos_BarcodeTextPrintPosition_BelowBarcode);
            AutoReplyPrint.INSTANCE.CP_Pos_PrintBarcode(h, AutoReplyPrint.CP_Pos_BarcodeType_CODE93, ""+resultGoodListBean.getMid());

            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");
            AutoReplyPrint.INSTANCE.CP_Pos_PrintText(h, "    \r\n");



            AutoReplyPrint.INSTANCE.CP_Pos_Beep(h, 1, 500);

            QueryPrintResult(h);
            AutoReplyPrint.INSTANCE.CP_Port_Close(h);
        }
    }




    private Pointer OpenPort() {
        Pointer h = Pointer.NULL;
        h = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x4B43,PID:0x3538", 1);

        if (h == Pointer.NULL) {
            scaleView.printMessage("OpenPort Failed");
        }
        return h;
    }

    private void ClosePort() {
        if (h != Pointer.NULL) {
            AutoReplyPrint.INSTANCE.CP_Port_Close(h);
            h = Pointer.NULL;
        }
    }

    private boolean QueryPrintResult(Pointer h) {
        boolean result = AutoReplyPrint.INSTANCE.CP_Pos_QueryPrintResult(h, 30000);
//        scaleView.printMessage(result ? "Print Success" : "Print Failed");
        scaleView.printMessage(result ? "打印成功" : "打印失败");
        if (!result) {
            LongByReference printer_error_status = new LongByReference();
            LongByReference printer__info_status = new LongByReference();
            LongByReference timestamp_ms_printer_status = new LongByReference();
            if (AutoReplyPrint.INSTANCE.CP_Printer_GetPrinterStatusInfo(h, printer_error_status, printer__info_status, timestamp_ms_printer_status)) {
                AutoReplyPrint.CP_PrinterStatus status = new AutoReplyPrint.CP_PrinterStatus(printer_error_status.getValue(), printer__info_status.getValue());
                String error_status_string = String.format("Printer Error Status: 0x%04X", printer_error_status.getValue() & 0xffff);
                if (status.ERROR_OCCURED()) {
                    if (status.ERROR_CUTTER())
                        error_status_string += "[ERROR_CUTTER]";
                    if (status.ERROR_FLASH())
                        error_status_string += "[ERROR_FLASH]";
                    if (status.ERROR_NOPAPER())
                        error_status_string += "[ERROR_NOPAPER]";
                    if (status.ERROR_VOLTAGE())
                        error_status_string += "[ERROR_VOLTAGE]";
                    if (status.ERROR_MARKER())
                        error_status_string += "[ERROR_MARKER]";
                    if (status.ERROR_ENGINE())
                        error_status_string += "[ERROR_ENGINE]";
                    if (status.ERROR_OVERHEAT())
                        error_status_string += "[ERROR_OVERHEAT]";
                    if (status.ERROR_COVERUP())
                        error_status_string += "[ERROR_COVERUP]";
                    if (status.ERROR_MOTOR())
                        error_status_string += "[ERROR_MOTOR]";
                }
                scaleView.printMessage(error_status_string);
            } else {
                scaleView.printMessage("CP_Printer_GetPrinterStatusInfo Failed");
            }
        }
        return result;
    }



}
